package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"furni-suite/furnirpc/furnirpc"
	"furni-suite/furnirpc/internal/svc"
	"furni-suite/models/cache"
	"furni-suite/models/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type FurnitureListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnitureListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnitureListLogic {
	return &FurnitureListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 家具列表
func (l *FurnitureListLogic) FurnitureList(in *furnirpc.FurnitureListShowRequest) (*furnirpc.FurnitureListResponse, error) {
	// todo: add your logic here and delete this line
	var data []*furnirpc.FurnitureList

	key := fmt.Sprintf("furn_list_%s", in)
	result, _ := cache.Rdb.Get(l.ctx, key).Result()
	if result == "" {
		data = mysql.FurnitureList(mysql.DB, in)
		marshal, _ := json.Marshal(data)
		cache.Rdb.Set(l.ctx, key, marshal, time.Minute*30)
	} else {
		json.Unmarshal([]byte(result), &data)
	}
	return &furnirpc.FurnitureListResponse{
		List: data,
	}, nil
}
